package hello.controller;

import hello.model.HelloBean;
import hello.model.MyThing;
import hello.service.HelloService;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author majinding888@foxmail.com
 * @date 2016-12-21 上午10:43:48
 */
@Controller
public class HelloController {

	@Autowired
	HelloService service;

	@RequestMapping("/hello/list")
	public String list(Model model) {
		model.addAttribute("list", service.queryHellos());
		return "list";
	}

	@RequestMapping("/hello/info")
	public String info(Integer id, Model model) {
		model.addAttribute("v", service.queryHello(id));
		return "info";
	}

	@RequestMapping("/hello")
	public String hello(Model model) {
		model.addAttribute("msg", "你好!");
		return "hello";
	}

	@RequestMapping(value = "/hello/json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Object helloBean() {
		HelloBean hello = new HelloBean();
		hello.setId(123);
		hello.setMsg("你好!");
		hello.setDate(new Date());
		return hello;
	}

	@RequestMapping(value = "/hello/xml", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
	@ResponseBody
	public Object xml() {
		MyThing m = new MyThing();
		m.setAge(12);
		m.setName("孙悟空");
		return m;
	}

}
